package gajdulewiczr;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class CamPosDialog extends JDialog {

	private static final long serialVersionUID = 8264579272614431249L;
	double[] cam = new double[5];
	JTextField[] texts = new JTextField[5];
	JButton submit;

	public CamPosDialog() {
		this.setSize(350, 100);
		this.getContentPane().setLayout(new FlowLayout());
		this.add(new JLabel("X"));
		texts[0] = new JTextField();
		this.add(texts[0]);
		this.add(new JLabel("Y"));
		texts[1] = new JTextField();
		this.add(texts[1]);
		this.add(new JLabel("Z"));
		texts[2] = new JTextField();
		this.add(texts[2]);
		this.add(new JLabel("Xrot"));
		texts[3] = new JTextField();
		this.add(texts[3]);
		this.add(new JLabel("yrot"));
		texts[4] = new JTextField();
		this.add(texts[4]);
		submit = new JButton("Zapisz");
		submit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				CamPosDialog.this.setVisible(false);
				Window window = SwingUtilities.windowForComponent( (Component)e.getSource() );
				WindowEvent windowClosing = new WindowEvent(window, WindowEvent.WINDOW_CLOSING);
				Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
				
			}
		});
		this.add(submit);
		
	}

	public void initDialog(double[] camPos) {
		if (cam.length != 5)
			throw new RuntimeException("Invalid cam pos!");
		this.cam = camPos;
		for (int i = 0; i < cam.length; i++) {
			texts[i].setText(Double.toString(cam[i]));
		}

	}

	public double[] getVals() {
		for (int i = 0; i < cam.length; i++) {
			cam[i] = Double.parseDouble(texts[i].getText());
		}
		return cam;
	}

}
